package com.tianbing.adlottery.controller.hordeadmin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;

import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.Site2Content;
import com.tianbing.adlottery.controller.AdLotterySimpleFormController;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditMyServedStoreCouponController extends AdLotterySimpleFormController {
	static Logger logger = Logger.getLogger(EditMyServedStoreCouponController.class.getName());

	private ISiteDao siteDao;

	private ISiteBusiness siteBusiness;

	private SkinUtils skinUtils;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		long s2csid = ServletRequestUtils.getLongParameter(request, "s2csid", -1);

		Site2Content s2c = siteDao.findSite2ContentBy(s2csid);
		if (s2c == null) {
			long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
			Site siteDetail = siteDao.findSiteBySysId(ssid);
			if (siteDetail != null) {
				s2c = new Site2Content();
				s2c.setSite(siteDetail);
				Content content = new Content();
				content.setStartDate(new Date());
				content.setEndDate(new Date());
				content.setTypeId(IConstants.Type.CONTENT_COUPON.getId());
				s2c.setContent(content);
			}
		}
		fb.setSite2Content(s2c);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		ModelAndView mv = new ModelAndView(this.getSuccessView() + "?ssid=" + ssid);
		ControllerFormBean form = (ControllerFormBean) cmd;
		Site2Content s2c = form.getSite2Content();

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		siteBusiness.saveUserServedStoreCoupon(auth.getUserSysId(), ssid, s2c.getContent());
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		long s2csid = ServletRequestUtils.getLongParameter(request, "s2csid", -1);
		Site siteDetail = siteDao.findSiteBySysId(ssid);

		model.put("siteDetail", siteDetail);
		model.put("ssid", ssid);
		model.put("s2csid", s2csid);

		return model;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public static void setLogger(Logger logger) {
		EditMyServedStoreCouponController.logger = logger;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

}
